home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / OOP_Course / Labs / IB_RPNCalculator / Solution / RPNCalculator.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  1.5 KB  |  88 lines

  1. /*Objective_C Implementation of the RPNCalculator Class: RPNCalculator.m*/
  2.  
  3. #import "RPNCalculator.h"
  4.  
  5. @implementation RPNCalculator : Object
  6.  
  7. //Initialize the displays by clearing them
  8. -appDidInit:sender
  9. {
  10.     [self clearEntry];
  11.     [display setFloatValue:0.0];
  12.     return self;
  13. }
  14.  
  15.  //Initialize a new RPNCalculator instance
  16. -init
  17. {
  18.     stack = [[Stack alloc] init];
  19.     return [super init];
  20. }    
  21.                                                                    
  22. // Enter a number - get it from the entry field
  23. - enter:sender
  24. {
  25.     [stack push:[entryField floatValue]];
  26.     [display setFloatValue: [stack top]];
  27.     [self clearEntry];
  28.     return self;
  29. }
  30.  
  31. //Add top values on stack and display result
  32. -add:sender{
  33.     [display setFloatValue: [self add]];
  34.     [self clearEntry];
  35.     return self;
  36. }
  37.  
  38. //Add two elements
  39. -(float)add{
  40.     [stack push: ( [stack  pop] +  [stack  pop]) ];
  41.     return [stack top];
  42. }
  43.  
  44. //Subtract top values on stack and display result
  45. -subtract:sender{
  46.     [display setFloatValue: [self subtract]];
  47.     [self clearEntry];
  48.     return self;
  49. }
  50.  
  51. //Subtract two elements
  52. -(float)subtract{
  53.     [stack push: ( -[stack  pop] +  [stack  pop]) ];
  54.     return [stack top];
  55. }
  56.  
  57. // Print the calculator's stack
  58. -printStack{
  59.     [stack printStack];
  60.     return self;
  61. }
  62.  
  63. // Clear the entry field
  64. -clearEntry
  65. {
  66.     [entryField setStringValue:""];
  67.     [entryField selectText:self];    //select entry again
  68.     return self;
  69. }
  70.  
  71. // Empty the calculator's stack
  72. -allClear:sender
  73. {
  74.     [stack empty];
  75.     [display setFloatValue: [stack top]];
  76.     [self clearEntry];
  77.     return self;
  78. }
  79.  
  80. // Free the calculator and its  stack
  81. -free
  82. {
  83.     [stack free];
  84.     return [super free];
  85. }
  86.  
  87. @end
  88.